<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Tintuc extends CI_Controller
{

	public	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		if($this->session->userdata('user_id')){
			//$this->load->helper("url");
			$this->load->model("tintucs");
			$this->load->model("tailieus");
			$this->load->model("giaobans");
			$this->load->model("users");			
			$this->load->model("lich");
			date_default_timezone_set('Asia/Ho_Chi_Minh');
		}
		else{
			redirect('login');
		}
	}
	public function index($msg = null, $parameters = null)
	{
		$data['menu'] = "thongbao";
		$data['list_tailieu'] = $this->tailieus->listTailieu();
		$data['list_giaoban'] = $this->giaobans->listGiaoban();
		$data['list_lich'] = $this->lich->listLich();
		$this->load->view('header_block_v1',$data);
		$this->load->view('content_sidebar_block',$data);
		switch ($msg){
			case null:
				$this->viewPageTintuc(0);
				break;
			case 'viewall':
				$this->viewPageTintuc($parameters);
				break;
			case 'view':
				$this->viewTintuc($parameters);
				break;
			case 'viewadd':
				$this->viewAddTintuc();
				break;
			case 'viewedit':
				$this->viewEditTintuc($parameters1);				
				break;
		}
		$this->load->view('footer_block');
	}
	public function viewPageTintuc($id=null)
	{
		$total_row = $this->tintucs->getSoluongTintuc();
		$data['allnew'] = $this->tintucs->getPage($id);
		$this->paging($total_row);
		$menu['menu'] = "thongbao";

		$this->load->view('tintuc/viewAllTintuc',$data);

	}
	public function viewTintuc($id_tintuc)
	{
		$data['new'] = $this->tintucs->getTintuc($id_tintuc);
		$this->load->view('tintuc/viewTintuc',$data);
	}
	
	public function viewAddTintuc()
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		if(in_array(3, $quyen)) {

			$this->load->view('tintuc/viewAddTintuc');

		}
		else
			redirect('tintuc');
	}
	public function viewEditTintuc($id_tintuc)
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		if(in_array(3, $quyen)) {
			$data['tintuc'] = $this->tintucs->getTintuc($id_tintuc);

			$this->load->view('tintuc/viewEditTintuc',$data);

		}
		else
			redirect('tintuc');
	}
	//function
	public function download($id_thongbao)
	{
		//echo "dang download...";
		//các file upload được để trong 1 thư mục riêng
		$upload_dir = "./filedinhkemthongbao/";
		//lấy tên file cần download từ URL
		$tintuc = $this->tintucs->getTintuc($id_thongbao);
		$name_tailieu = $tintuc->tailieu_tintuc."- Tai lieu dinh kem thong bao ".$tintuc->tieude_tintuc;
		$filename = $tintuc->tailieu_tintuc;
		$ext = explode('.', $filename);

		//thực hiện quá trình kiểm tra
		if (!is_file($upload_dir.$filename) || !is_readable($upload_dir.$filename) ) {
			///echo $upload_dir.$filename;
			echo "Loi: Ten file khong hop le hoac file khong ton tai!";
			exit(-1);
		} //end if
		$fp = fopen($upload_dir.$filename, "rb");
		//gởi header đến cho browser
		header('Content-type: application/octet-stream');
		if($ext[1] != '')
			header('Content-disposition: attachment; filename="'.$name_tailieu.'.'.$ext[1].'"');
		else
			header('Content-disposition: attachment; filename="'.$name_tailieu.'"');
		header('Content-length: ' . filesize($upload_dir.$filename));
		//đọc file và trả dữ liệu về cho browser
		fpassthru($fp);
		fclose($fp);
	}

	private function upload($userfile)
	{
		date_default_timezone_set('UTC');
		$create_date = date("YmdHis");

		$config['upload_path'] = './filedinhkemthongbao/';
		$config['allowed_types'] = '*';
		//$config['file_name'] = 'tintucs_'.$create_date;
		$config['overwrite'] = false;
		$config['max_size']	= '10048';			//<10MB
		//$config['max_width']  = '1024';			//<1024px
		//$config['max_height']  = '768';			//<768px

		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if(!$this->upload->do_upload($userfile))
		{
			$error = $this->upload->display_errors('', '');
			?>
			<script type="text/javascript">	
				   alert('<?php echo $error;?>');				   
			</script>
<?php	
$upload_data = $this->upload->data($userfile);
return null;			//loi
		}
		$upload_data = $this->upload->data($userfile);

		//print_r($upload_data);

		$news_img = $upload_data['file_name'];
		return $news_img;
	}
	
	
	public function addTintuc()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề thông báo', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung thông báo', 'required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewAddTintuc();
		}
		else {
			if($_FILES['upload_tailieu']['name'])
				$data['tailieu_tintuc'] = $this->upload('upload_tailieu');
			
			$data["tieude_tintuc"] = $this->input->post("tieude_tin");
			$data["noidung_tintuc"] = $this->input->post("noidung_tin");
			$data["ngay_tintuc"] = date("Y-m-d H:i:s");
			$data["id_nguoidung"] = $this->session->userdata("user_id");
			$url = base_url()."tintuc";
			if($this->tintucs->insertTintuc($data)){
				echo "<script>alert('Bạn đã thêm 1 thông báo mới');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}

	public function editTintuc($id_tintuc)
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề thông báo', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung thông báo', 'required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewEditTintuc($id_tintuc);
		}
		else {
			if($_FILES['upload_tailieu']['name'])
				$data['tailieu_tintuc'] = $this->upload('upload_tailieu');
			$data["tieude_tintuc"] = $this->input->post("tieude_tin");
			$data["noidung_tintuc"] = $this->input->post("noidung_tin");
			$data["ngay_tintuc"] = date("Y-m-d H:i:s");
			$data["id_nguoidung"] = $this->session->userdata("user_id");
			$url = base_url()."tintuc";
			if($this->tintucs->updateTintuc($data,$id_tintuc)){
				echo "<script>alert('Bạn đã sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delTintuc($id_tintuc)
	{
		$url = base_url()."tintuc";
		if($this->tintucs->delTintuc($id_tintuc)){
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}

	function paging($total_row){
		$this->load->library('pagination');
		$config['base_url']=site_url("tintuc/viewPageTintuc");
		$config['total_rows']=$total_row;
		$config['per_page']=15;
		$config['full_tag_open'] = "<div class='news_pagination'>";
		$config['full_tag_close'] = '</div>';
		$config['first_link'] = 'Trang đầu';
		$config['first_tag_open'] = "<div class='news_page next'>";
		$config['first_tag_close'] = '</div>';
		$config['last_link'] = 'Trang cuối';
		$config['last_tag_open'] = "<div class='news_page next'>";
		$config['last_tag_close'] = '</div>';
		$config['next_link'] = 'Trang tiếp';
		$config['next_tag_open'] = "<div class='news_page next'>";
		$config['next_tag_close'] = '</div>';
		$config['prev_link'] = 'Trang trước';
		$config['prev_tag_open'] = "<div class='news_page next'>";
		$config['prev_tag_close'] = '</div>';
		$config['cur_tag_open'] = "<div class='news_page number curr_page'>";
		$config['cur_tag_close'] = '</div>';
		$config['num_tag_open'] = "<div class='news_page number'>";
		$config['num_tag_close'] = '</div>';
		$this->pagination->initialize($config);
	}

}